<?php
class Session
{

    public $proxy = "";
    public $ip = "";
    public $agent;
    public $version;
    public $platform;
    public $language = array();
    public $country = array();

    public function Session( )
    {
        $this->GetBrowserInfo();
    }

    public function GetBrowserInfo( )
    {
        $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : "";
        $langs = split( ",", $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
        foreach ( $langs as $lang )
        {
            $this->language[] = $lang;
        }
        if ( ereg( "MSIE ([0-9].[0-9]{1,2})", $useragent, $log_version ) )
        {
            $this->version = $log_version[1];
            $this->agent = "IE";
        }
        else if ( ereg( "Opera ([0-9].[0-9]{1,2})", $useragent, $log_version ) )
        {
            $this->version = $log_version[1];
            $this->agent = "OPERA";
        }
        else if ( ereg( "Mozilla/([0-9].[0-9]{1,2})", $useragent, $log_version ) )
        {
            $this->version = $log_version[1];
            $this->agent = "MOZILLA";
        }
        else
        {
            $this->version = 0;
            $this->agent = "OTHER";
        }
        if ( strstr( $useragent, "Win" ) )
        {
            $this->platform = "Win";
        }
        else if ( strstr( $useragent, "Mac" ) )
        {
            $this->platform = "Mac";
        }
        else if ( strstr( $useragent, "Linux" ) )
        {
            $this->platform = "Linux";
        }
        else if ( strstr( $useragent, "Unix" ) )
        {
            $this->platform = "Unix";
        }
        else
        {
            $this->platform = "Other";
        }
        if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
        {
            $this->proxy = $_SERVER['REMOTE_ADDR'];
            $this->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $this->proxy = "";
            $this->ip = $_SERVER['REMOTE_ADDR'];
        }
    }

}

function sess_open( $save_path, $session_name )
{
    return true;
}

function sess_close( )
{
    return true;
}

function sess_read( $key )
{
    $value = "";
    $sql = "select value from k_sessions where sid='{$key}'";
    $rs = getdb( $sql );
    if ( !$rs->eof )
    {
        $value = $rs->f['value'];
    }
    else
    {
        sess_insert( $key );
    }
    return $value;
}

function sess_write( $key, $val )
{
	global $CORE;
    $value = addslashes( $val );
	//if(empty($value)) return 1;
    $sql = "select value from k_sessions where sid='{$key}'";
    $rs = getrs( $sql );
    if (  $rs)
    {
		//db_update("k_sessions",array("value"=>$value,"lastupdate"=>systime()),"sid='{$key}'");
		//echo "update k_sessions set value='{$value}',lastupdate=".systime( )." WHERE sid='{$key}'" ;
        $CORE['db']->query( "update k_sessions set value='{$value}',lastupdate=".systime( )." WHERE sid='{$key}'" );
        return 1;
    }
    sess_insert( $key, $val );
    return 1;
}

function sess_destroy( $key )
{
    $sql = "delete from k_sessions where sid='{$key}'";
    getdb( $sql );
    return $qid;
}

function sess_gc( $maxlifetime = 0 )
{
	
    if ( $maxlifetime == 0 )
    {
        $maxlifetime = get_cfg_var( "session.gc_maxlifetime" );
        $sql = "delete from k_sessions where lastupdate<".( systime( ) - $maxlifetime );
    }
    else
    {
        $maxlifetime = min( $maxlifetime, get_cfg_var( "session.gc_maxlifetime" ) );
        $sql = "delete from k_sessions where lastupdate<".( systime( ) - $maxlifetime );
    }
    getdb( $sql );
    return true;
}

function sess_insert( $sid, $val = "" )
{
    $proxy = "";
    $ip = $_SERVER['REMOTE_ADDR'];
    if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
    {
        $proxy = $_SERVER['REMOTE_ADDR'];
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    $host = isset( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['HTTP_HOST'];
    $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : "";
    $uri = "";
    $sql = "insert into k_sessions (proxy,ip,host,agent,uri,sid,value,lastupdate,createtime) values ("."'{$proxy}','{$ip}','{$host}','{$agent}','{$uri}','{$sid}','{$val}',".systime( ).",".systime( ).")";
	getdb( $sql );
}

function sess_update_uri( )
{
    db_update( "k_sessions", array( "uri" => "'".get_uri( )."'" ), "sid='".session_id( )."'" );
}
session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc" );
session_start();
sess_update_uri();
?>